<template>
    <div>
        <!--卡片-->
        <el-card class="box-card">
            <!--定义面包屑-->
            <el-breadcrumb separator=">">
                <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>维修管理</el-breadcrumb-item>
                <el-breadcrumb-item>维修厂管理</el-breadcrumb-item>
            </el-breadcrumb>
            <el-divider></el-divider>
            <!--表单的搜索框
                 :model="双向绑定表单的数据"
            -->
            <el-form :inline="true" :model="mtShopFormData"  class="demo-form-inline">
                <el-form-item >
                    <el-input v-model="mtShopFormData.shopName" placeholder="请输入名称"></el-input>
                </el-form-item>
                <el-form-item >
                    <el-date-picker
                            v-model="mtShopFormData.startDate"
                            type="date"
                            placeholder="选择开始日期"
                            :picker-options="pickerOptions">
                    </el-date-picker>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary"  @click="initmtShopTable">查询</el-button>
                    <el-button type="primary" @click="editMtShopDialog=true">添加</el-button>
                </el-form-item>
            </el-form>

            <!--数据表格-->
            <template>
                <el-table
                        :data="tableData"
                        :border="true"
                        stripe
                        style="width: 100%">
                    <!--<el-table-column
                            prop="id"
                            label="编号"
                            width="180">
                    </el-table-column>-->
                    <el-table-column
                            prop="name"
                            label="维修厂名称"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="address"
                            label="维修厂地址">
                    </el-table-column>
                    <el-table-column
                            prop="addtime"
                            label="成立时间">
                    </el-table-column>
                    <el-table-column
                            prop="phoneNum"
                            label="维修电话">
                    </el-table-column>
                    <!--<el-table-column
                            prop="operator"
                            label="操作人">
                    </el-table-column>
                    <el-table-column
                            prop="operationTime"
                            label="操作时间">
                    </el-table-column>-->
                    <el-table-column
                            prop="status"
                            label="状态">
                        <template slot-scope="scope">
                            <el-switch
                                    v-model="scope.row.status"
                                    active-value="0"
                                    active-color="#13ce66"
                                    inactive-value="1"
                                    inactive-color="#ff4949"
                                    @change="switchStatus(scope.row.id,scope.row.status)">
                            </el-switch>
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop="remark"
                            label="详情">
                    </el-table-column>

                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button type="primary" icon="el-icon-edit" @click="selectById(scope.row.id)" circle></el-button>
                            <el-button type="danger" icon="el-icon-delete" @click="del(scope.row.id)" circle></el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <!--分页-->
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="currentPage"
                        :page-sizes="[5, 10, 15, 20]"
                        :page-size="7"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>
            </template>

        </el-card>
        <!--                更新用户信息表单-->
        <el-dialog
                title="添加维修厂"
                :visible.sync="editMtShopDialog"
                width="30%">
            <!--            model表单数据-->
            <el-form  label-width="80px" :model="mtShopForm" :rules="mtShopFormRules" ref="mtShopFormRef">
                <el-form-item label="维修厂名字" prop="name">
                    <el-input v-model="mtShopForm.name"></el-input>
                </el-form-item>
                <el-form-item label="维修厂地址" prop="address">
                    <el-input v-model="mtShopForm.address"></el-input>
                </el-form-item>
                <el-form-item label="成立时间" prop="addtime">

                    <el-input v-model="mtShopForm.addtime" type="date" placeholder="选择成立日期"></el-input>
                </el-form-item>
                <el-form-item label="维修电话" prop="phoneNum">
                    <el-input v-model="mtShopForm.phoneNum"></el-input>
                </el-form-item>
                <el-form-item label="备注" >
                    <el-input v-model="mtShopForm.remark"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                        <el-button @click="editMtShopDialog = false">取 消</el-button>
                        <el-button type="primary" @click="editMtShopBtn">确 定</el-button>
                    </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "MaintainShop",
        data(){
            //重复名自定义校验
            let validateAnotherMtShopName = (rule, value, callback) => {
                //后台方法
                this.$http.post(`/maintain/shop/selMtShopName`,this.mtShopForm).then(function (res) {
                    if (res && res.data.result.code === 0) {

                        callback()
                    } else if (res && res.data.result.code === 1) {
                        callback(new Error('该厂已录入'))
                    }

                })
            }
            //数字自定义校验
            const validateAcquaintance = (rule, value, callback) => {
                if (!value) {
                    callback(new Error('必须输入熟悉程度'))
                }
                value = Number(value)
                if (typeof value === 'number' && !isNaN(value)) {
                        callback()
                } else {
                    callback(new Error('必须为数字'))
                }
            }
            return {
                mtShopFormData:{},
                tableData:[],
                currentPage:1,
                pageSize:5,
                total:0,
                //添加用户弹出框显示状态
                editMtShopDialog:false,
                //添加用户表单信息
                mtShopForm:{},
                // 表单校验数据:
                mtShopFormRules:{
                    name:[{required:true,message:"名称不可为空",trigger:'blur'},
                        { validator: validateAnotherMtShopName, trigger: 'blur'},],
                    address:[{required:true,message:"地址不可为空",trigger:'blur'}],
                    addtime:[{required:true,message:"必须选择时间",trigger:'blur'}],
                    phoneNum:[{ required: true, message: '电话不可为空',trigger: 'blur'},
                        { validator: validateAcquaintance, trigger: 'blur'}
                        ,{pattern: /^[0-9]*$/,
                            message: "只能输入数字"}
                        ,{
                            min:8,
                            max:11,
                            message: "长度8到11"
                        }]
                },
                //日期选择时间左边快捷键
                pickerOptions: {
                    disabledDate(time)
                    {
                        //return time.getTime() > Date.now() - 8.64e7;//后面的-8.64e7就是不可以选择今天的
                        return time.getTime() > Date.now();//可以选择当前时间
                    }
                }
            }
        },
        created() {
            this.initmtShopTable();
        },
        methods:{

            initmtShopTable(){
                //get方式不支持json数据
                var that=this;
                this.$http.post(`/maintain/shop/initMtShopTable/${this.currentPage}/${this.pageSize}`,this.mtShopFormData).then(function(resp){
                    that.tableData=resp.data.result.records;
                    // console.log(resp.data.result.records)
                    that.total=resp.data.result.total;
                });
            },
            //状态的改变
            switchStatus(id,status){
            var that=this;
                this.$http.put(`/maintain/shop/up/${id}/${status}`).then(function (re) {
                    if (status==="1"){
                        that.$message.warning("终止合作")
                    }else {
                        that.$message.success("开始合作")
                    }
                })
            },
            //分页
            handleSizeChange(v){
                this.pageSize=v;
                this.initmtShopTable();
            },
            //分页
            handleCurrentChange(v){
                this.currentPage=v;
                this.initmtShopTable();
            },
            //添加
            editMtShopBtn(){
                var  that=this;
                var username=sessionStorage.getItem("username");
                this.$refs.mtShopFormRef.validate((valid)=>{
                    if (valid){//验证通过
                        //进行id判断,如果id不为空则回显数据,走修改,如果id为空,则走添加
                        if (this.mtShopForm.id!=null) {
                            //有id就修改
                            this.$http.put(`/maintain/shop/updateMtShopById/`, this.mtShopForm).then(function (r) {
                                // that.$message.success(r.data.message);
                                //消息通知
                                that.$message.success("修改成功");
                                //提交后关闭弹窗
                                that.editMtShopDialog = false;
                                //提交后刷新
                                that.initmtShopTable();


                            })
                        }else {
                            //没有id就插入
                            this.$http.post(`/maintain/shop/insertMtShop/${username}`,this.mtShopForm).then(function (r) {
                                // that.$message.success(r.data.message);
                                //提交后关闭弹窗
                                that.editMtShopDialog=false;
                                //提交后刷新
                                that.initmtShopTable();
                                //消息通知
                                that.$message({
                                    message: '添加成功',
                                    type: 'success'
                                });
                            })
                        }
                         that.mtShopForm={};

                    }else {
                        this.$message.error('好好填一下信息呀,亲');
                    }
                })

            },
            //删除
            del(id){
                var that=this;
                this.$http.delete(`/maintain/shop/del/${id}`).then(function (resp) {
                    that.$message.success("删除成功");
                    //提交后刷新
                    that.initmtShopTable();
                })
            },
            //修改回显
            selectById(id){
                this.editMtShopDialog=true;
                var that=this;
                this.$http.get(`/maintain/shop/selById/${id}`).then(function (resp) {
                    that.mtShopForm=resp.data.result.MtShop;
                })
            },
        }
    }
</script>

<style scoped>

</style>